<?php
require_once 'BaseController.php';

class SecurityController extends BaseController
{

    public function init()
    {
//        $MyFunction = new MyFunctions();
//        $this->view->myfunction = $MyFunction;
        parent::init();
        //TODO review all this function
        $this->session = new Zend_Session_Namespace('user');
        $this->auth = Zend_Auth::getInstance();

        $this->view->security = $this;
        $this->view->user = $this->getUser();
        //TODO chenge by the farm
        //$this->view->organization_name = $this->getOrganizationName();
        //Zend_Layout::getMvcInstance()->setLayout('layout_back');
    }


    public function checkAction($value, $redirect = false)
    {
        $this->isSomeOne($redirect);

        if ($this->getRoles() != null) {
            $roles = $this->getRoles();
            foreach ($roles as $rol) {
                if (strcmp($value, $rol) == 0) {
                    return true;
                }
            }
        }
        if ($redirect) {
            $this->_redirect('/login/denied');
        }

        return false;
    }


    public function initializeAction()
    {

        $user = $this->getUser();
        $profile = $user->getPerfil();
        $roles = array();

        switch ($profile) {

            case self::PROFILE_ADMIN:
                array_push($roles, self::ROL_FARM_CREATE);
                array_push($roles, self::ROL_SHOW_USERS);
                break;
        }

        $this->setRoles($roles);
    }

    public function isAdmin()
    {
        return $this->getUser()->getPerfil() == self::PROFILE_ADMIN;
    }

    public function isSomeOne($redirect = false)
    {
        $f = $this->getUser() != null ? true : false;
        if (!$f && $redirect) {
            $this->_redirect('/login');
            exit;
        }
        return $f;
    }

    /**
     * @return Zend_Session_Namespace
     */
    private function getSession()
    {
        return new Zend_Session_Namespace('porci');
    }

    /**
     * retorna usuario en session usuario
     * @return Application_Model_UsuarioGranja
     */
    public function getUser()
    {
        $auth = Zend_Auth::getInstance()->getIdentity();
        return $auth;
    }

    public function getRoles()
    {
        return $this->getSession()->roles;
    }

    public function setRoles($roles)
    {
        $this->getSession()->roles = $roles;
    }

    /**
     * @return Application_Model_Granja
     */
    public function getGranja()
    {
        return $this->getSession()->granja;
    }

    public function setGranja($granja)
    {
        $this->getSession()->granja = $granja;
    }

    public function cleanAction()
    {
        $this->getSession()->roles ="";
        $this->getSession()->granja ="";
        Zend_Auth::getInstance()->clearIdentity();
//        Zend_Session::destroy(true);

        //TODO clear all asession's namespaces
    }


//Profiles
    const PROFILE_ADMIN = 'admin';
    const PROFILE_ASESOR = 'asesor';
    const PROFILE_GRANJERO = 'granjero';

//Roles
    const ROL_FARM_CREATE = 'farm_create';
    const ROL_SHOW_USERS = 'show_users';
}







